#ifndef _QY_THREAD_H
#define _QY_THREAD_H

#include <pthread.h>

namespace qy
{

class Event;

class Thread
{
public:
    Thread();
	virtual ~Thread();

	bool start();

	//just tell the thread to stop, but don't requre it must stop immediately,
	//if it doesn't stop after seconds, kill it
//	void stop(unsigned int seconds);
	void stop();

	//kill this thread immediately
	bool kill();

	//wait until this thread dies
	void join();

	//return true if this thread is running
	bool isRunning();

	//call e->wait() to wait until this thread starts running
	void setStartEvent(Event *e);

	//call e->wait() to wait until this thread dies
	void setFinishedEvent(Event *e);

	//return thread id
	pthread_t threadId(); 

protected:
	virtual void beforeRun();
	virtual int run() = 0;
	virtual void afterRun();
	bool m_stop;

private:
    static void *_start(void *arg);

private:
	bool m_isRunning;
	pthread_t m_threadId;
	Event *m_startEvent;
	Event *m_finishedEvent;
};

}

#endif
